package com.example.activity.lifecycle;

import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    private final String TAG = "MainActivity";

    private TextView tvName;

    // The activity is being created.
    // Activity的整个生命周期发生在onCreate()与onDestroy()之间
    // onCreate()完成全局的状态设置，如：定义布局，并释放onDestroy()中的所有其余资源
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 设置横竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
        setContentView(R.layout.activity_main);

        Log.i(TAG, "onCreate() is called");

        tvName = findViewById(R.id.tv_name);

    }

    // The activity is about to become visible.
    // Activity的可见生命周期发生在onStart()与onStop()之间
    // Activity变得可见，并可以交互
    @Override
    protected void onStart() {
        super.onStart();
        Log.i(TAG, "onStart() is called");
    }

    // The activity has become visible (it is now "resumed").
    // Activity的前台生命周期发生在onResume()和onPause()之间
    // 在此期间，Activity位于屏幕的所有Activity之前，并具有用户输入焦点
    @Override
    protected void onResume() {
        super.onResume();
        Log.i(TAG, "onResume() is called");
    }

    // Another activity is taking focus (this activity is about to be "paused").
    @Override
    protected void onPause() {
        super.onPause();
        Log.i(TAG, "onPause() is called");
    }

    // The activity is no longer visible (it is now "stopped")
    @Override
    protected void onStop() {
        super.onStop();
        Log.i(TAG, "onStop() is called");
    }

    // The activity is about to be destroyed.
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy() is called");
    }

    @Override
    protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        String name = savedInstanceState.getString("name");
        tvName.setText(name);

        Log.i(TAG, "onRestoreInstanceState");
    }

    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("name", "John");

        Log.i(TAG, "onSaveInstanceState");
    }

    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        Log.i(TAG, "onConfigurationChanged");
        initChange();
    }

    private void initChange(){
        if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
            Log.i(TAG, "屏幕改变，当前为竖屏");
        else
            Log.i(TAG, "屏幕改变，当前为横屏");
    }
}
// 横竖屏切换
//        if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
//            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//        } else {
//            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//        }
